/*     */ package com.zimbra.qa.unittest.prov.ldap;
/*     */ 
/*     */ import com.google.common.collect.Maps;
/*     */ import com.zimbra.common.account.Key.AccountBy;
/*     */ import com.zimbra.common.service.ServiceException;
/*     */ import com.zimbra.common.util.ByteUtil;
/*     */ import com.zimbra.cs.account.Account;
/*     */ import com.zimbra.cs.account.AuthToken;
/*     */ import com.zimbra.cs.account.Cos;
/*     */ import com.zimbra.cs.account.Domain;
/*     */ import com.zimbra.cs.account.Provisioning;
/*     */ import com.zimbra.cs.service.AuthProvider;
/*     */ import com.zimbra.cs.service.FileUploadServlet;
/*     */ import com.zimbra.cs.service.FileUploadServlet.Upload;
/*     */ import java.io.ByteArrayInputStream;
/*     */ import java.security.SecureRandom;
/*     */ import java.util.Arrays;
/*     */ import java.util.Map;
/*     */ import org.junit.AfterClass;
/*     */ import org.junit.Assert;
/*     */ import org.junit.BeforeClass;
/*     */ import org.junit.Ignore;
/*     */ import org.junit.Test;
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ public class TestProvZimbraId
/*     */   extends LdapTest
/*     */ {
/*  42 */   private static final String ZIMBRA_ID = "1234567890@" + genTestId();
/*     */   private static LdapProvTestUtil provUtil;
/*     */   private static Provisioning prov;
/*     */   private static Domain domain;
/*     */   
/*     */   @BeforeClass
/*     */   public static void init() throws Exception
/*     */   {
/*  50 */     provUtil = new LdapProvTestUtil();
/*  51 */     prov = provUtil.getProv();
/*  52 */     domain = provUtil.createDomain(baseDomainName());
/*     */   }
/*     */   
/*     */   @AfterClass
/*     */   public static void cleanup() throws Exception {
/*  57 */     Cleanup.deleteAll(new String[] { baseDomainName() });
/*     */   }
/*     */   
/*     */   @Test
/*     */   public void createAccountWithZimbraId() throws Exception {
/*  62 */     String zimbraId = ZIMBRA_ID;
/*     */     
/*  64 */     Map<String, Object> attrs = Maps.newHashMap();
/*  65 */     attrs.put("zimbraId", zimbraId);
/*  66 */     Account acct = provUtil.createAccount(genAcctNameLocalPart(), domain, attrs);
/*  67 */     Assert.assertEquals(zimbraId, acct.getId());
/*     */     
/*     */ 
/*  70 */     Account acctById = prov.get(Key.AccountBy.id, zimbraId);
/*  71 */     Assert.assertNotNull(acctById);
/*     */   }
/*     */   
/*     */   @Test
/*     */   public void createAccountWithInvalidZimbraId() throws Exception {
/*  76 */     String zimbraId = "containing:colon";
/*     */     
/*  78 */     Map<String, Object> attrs = Maps.newHashMap();
/*  79 */     attrs.put("zimbraId", zimbraId);
/*     */     
/*  81 */     boolean caughtException = false;
/*     */     try {
/*  83 */       acct = provUtil.createAccount(genAcctNameLocalPart(), domain, attrs);
/*     */     } catch (ServiceException e) { Account acct;
/*  85 */       Assert.assertEquals("service.INVALID_REQUEST", e.getCode());
/*  86 */       caughtException = true;
/*     */     }
/*  88 */     Assert.assertTrue(caughtException);
/*     */   }
/*     */   
/*     */   @Test
/*     */   @Ignore
/*     */   public void createAccountWithCosName() throws Exception
/*     */   {
/*  95 */     String cosName = genCosName();
/*  96 */     Cos cos = provUtil.createCos(cosName);
/*     */     
/*  98 */     Map<String, Object> attrs = Maps.newHashMap();
/*  99 */     attrs.put("zimbraCOSId", cosName);
/* 100 */     Account acct = provUtil.createAccount(genAcctNameLocalPart(), domain, attrs);
/*     */     
/* 102 */     Cos acctCos = prov.getCOS(acct);
/* 103 */     Assert.assertEquals(cos.getName(), acctCos.getName());
/* 104 */     Assert.assertEquals(cos.getId(), acctCos.getId());
/*     */   }
/*     */   
/*     */   @Test
/*     */   public void createAccountWithCosId() throws Exception
/*     */   {
/* 110 */     String cosName = genCosName();
/* 111 */     Cos cos = provUtil.createCos(cosName);
/*     */     
/* 113 */     Map<String, Object> attrs = Maps.newHashMap();
/* 114 */     attrs.put("zimbraCOSId", cos.getId());
/* 115 */     Account acct = provUtil.createAccount(genAcctNameLocalPart(), domain, attrs);
/*     */     
/* 117 */     Cos acctCos = prov.getCOS(acct);
/* 118 */     Assert.assertEquals(cos.getName(), acctCos.getName());
/* 119 */     Assert.assertEquals(cos.getId(), acctCos.getId());
/*     */   }
/*     */   
/*     */   @Test
/*     */   public void testFileUpload() throws Exception {
/* 124 */     Account acct = provUtil.createAccount(genAcctNameLocalPart(), domain);
/*     */     
/* 126 */     int bodyLen = 128;
/* 127 */     byte[] body = new byte[bodyLen];
/* 128 */     SecureRandom sr = new SecureRandom();
/* 129 */     sr.nextBytes(body);
/*     */     
/* 131 */     FileUploadServlet.Upload ulSaved = FileUploadServlet.saveUpload(new ByteArrayInputStream(body), "zimbraId-test", "text/plain", acct.getId());
/*     */     
/*     */ 
/*     */ 
/* 135 */     AuthToken authToken = AuthProvider.getAuthToken(acct);
/* 136 */     FileUploadServlet.Upload ulFetched = FileUploadServlet.fetchUpload(acct.getId(), ulSaved.getId(), authToken);
/*     */     
/*     */ 
/* 139 */     Assert.assertEquals(ulSaved.getId(), ulFetched.getId());
/* 140 */     Assert.assertEquals(ulSaved.getName(), ulFetched.getName());
/* 141 */     Assert.assertEquals(ulSaved.getSize(), ulFetched.getSize());
/* 142 */     Assert.assertEquals(ulSaved.getContentType(), ulFetched.getContentType());
/* 143 */     Assert.assertEquals(ulSaved.toString(), ulFetched.toString());
/*     */     
/* 145 */     byte[] bytesUploaded = ByteUtil.getContent(ulFetched.getInputStream(), -1);
/* 146 */     Assert.assertTrue(Arrays.equals(body, bytesUploaded));
/*     */   }
/*     */ }


/* Location:              /home/mint/zimbrastore.jar!/com/zimbra/qa/unittest/prov/ldap/TestProvZimbraId.class
 * Java compiler version: 7 (51.0)
 * JD-Core Version:       0.7.1
 */